<template>
  <!-- 虚假地标 -->
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryParams"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="虚假IP地址" prop="fakeIp">
        <el-input v-model="queryParams.fakeIp" clearable></el-input>
      </el-form-item>
      <el-form-item label="虚假位置名称" prop="fakeLocation">
        <el-input v-model="queryParams.fakeLocation" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      :default-sort="{ prop: 'date', order: 'descending' }"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="虚假IP地址" width="150" show-overflow-tooltip sortable>
        <template slot-scope="scope">
          {{ scope.row.fakeIp }}
        </template>
      </el-table-column>
      <el-table-column
        prop="fakeLocation"
        label="虚假位置名称"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="fakeLon"
        label="虚假位置经度"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="fakeLat"
        label="虚假位置纬度"
        show-overflow-tooltip
        sortable
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
        show-overflow-tooltip
      />

      <el-table-column
        prop="operation"
        label="操作"
        width="250"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            style="color: green"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            style="color: red"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentPageChange"
      :current-page="queryParams.pageNum"
      :page-size="queryParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      style="margin-top: 20px; text-align: center"
    >
    </el-pagination>
    <!-- 新增弹窗 -->

    <!-- 删除弹窗 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <span>确认删除这些任务吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      :title="title + '设备'"
      :visible.sync="addDialogVisible"
      width="50%"
    >
      <div class="apk-detail-content">
        <el-card class="box-card">
          <el-form
            ref="formEquip"
            :model="formEquip"
            :rules="rules"
            label-width="110px"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item required label="虚假IP地址" prop="fakeIp">
                  <el-input v-model="formEquip.fakeIp"></el-input>
                </el-form-item>
                <el-form-item required label="虚假位置名称" prop="fakeLocation">
                  <el-input
                    v-model="formEquip.fakeLocation"
                  ></el-input> </el-form-item
              ></el-col>
              <el-col :span="12">
                <el-form-item required label="虚假位置经度" prop="fakeLon">
                  <el-input v-model="formEquip.fakeLon"></el-input>
                </el-form-item>
                <el-form-item required label="虚假位置纬度" prop="fakeLat">
                  <el-input v-model="formEquip.fakeLat"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" size="small"
                >提交</el-button
              >
              <el-button size="small" @click="cancel">取消</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  fakeIpList,
  fakeIpEdit,
  fakeIpAdd,
  delEquipTask,
} from "@/api/networkEquipment/virtualPositioning";
export default {
  dicts: ["ip_city", "sys_user_sex"],
  data() {
    return {
      selectedOption1: "",
      selectedOption2: "",
      selectedOption3: "",
      input1: "",
      input2: "",
      input3: "",
      input4: "",
      input5: "",
      input6: "",
      activeName: "first",
      dialogVisible: false,
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示的数据条数
      totalDataCount: 0, // 总数据量，初始为0，将在数据加载时设置
      selectedRows: [], // 存放选中的行
      tableData: [],
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        noticeTitle: undefined,
        createBy: undefined,
        status: undefined,
      },
      total: 0,
      addDialogVisible: false,
      formEquip: {},
      rules: {
        fakeIp: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        ],
        fakeLocation: [
          { required: true, message: "请输入设备类型", trigger: "blur" },
        ],
        fakeLon: [
          { required: true, message: "请输入所属单位", trigger: "blur" },
        ],
        fakeLat: [
          { required: true, message: "请输入设备IP地址", trigger: "blur" },
        ],
        equipAddress: [
          { required: true, message: "请输入归属地", trigger: "blur" },
        ],
        equipLon: [
          { required: true, message: "请输入真实坐标经度", trigger: "blur" },
        ],
        equipLat: [
          { required: true, message: "请输入真实坐标纬度", trigger: "blur" },
        ],
        equipCity: [
          { required: true, message: "请输入设备城市", trigger: "blur" },
        ],
        equipArea: [
          { required: true, message: "请输入设备城区", trigger: "blur" },
        ],
      },
      title: "",
    };
  },
  methods: {
    // 编辑
    handleEdit(row) {
      console.log(row);
      this.title = "编辑";
      this.addDialogVisible = true;
      this.formEquip = JSON.parse(JSON.stringify(row));
    },
    // 取消
    cancel() {
      this.resetForm("formEquip");
      this.addDialogVisible = false;
    },
    // 提交
    onSubmit() {
      this.$refs["formEquip"].validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            fakeIpAdd(this.formEquip).then((res) => {
              if (res.code == 200) {
                this.getList();
                this.addDialogVisible = false;
                this.$message({
                  type: "success",
                  message: "新增成功!",
                });
              }
            });
          } else {
            fakeIpEdit(this.formEquip).then((res) => {
              if (res.code == 200) {
                this.getList();
                this.addDialogVisible = false;
                this.$message({
                  type: "success",
                  message: "编辑成功!",
                });
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 新增
    handleAdd() {
      // this.resetForm("formEquip");
      this.formEquip = {};
      console.log(this.formEquip);
      this.addDialogVisible = true;
      this.title = "新增";
    },
    // 重置
    resetQuery() {
      this.resetForm("queryParams");
      this.getList();
    },
    // 检索
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.getList();
    },
    // 获取列表
    getList() {
      fakeIpList(this.queryParams).then((res) => {
        this.tableData = res.rows || [];
        this.total = res.total;
      });
    },
    handleClick() {
      // 处理选项卡点击事件
    },

    handleSelectionChange(rows) {
      this.selectedRows = rows;
    },

    handleDownload() {
      // 处理下载按钮点击事件
    },
    handleSizeChange(newSize) {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = newSize;
      this.getList();
    },
    handleCurrentPageChange(newPage) {
      this.queryParams.pageNum = newPage;
      this.getList();
    },
    handleBatchDelete() {
      if (this.selectedRows.length === 0) {
        this.$message({
          message: "请至少选择一个设备进行删除",
          type: "warning",
        });
        return;
      }

      this.$confirm("确认删除这些设备吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let ids = [];
          this.selectedRows.map((ele) => {
            ids.push(ele.id);
          });
          delEquipTask(ids).then((res) => {
            if (res.code == 200) {
              this.getList();
              this.$message({
                type: "success",
                message: "批量删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            message: "已取消批量删除",
            type: "info",
          });
        });
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delEquipTask(row.id).then((res) => {
            if (res.code == 200) {
              this.getList();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "equip/info/export",
        {
          ...this.queryParams,
        },
        `application_${new Date().getTime()}.xlsx`
      );
    },
  },
  computed: {
    currentTableData() {
      let start = (this.currentPage - 1) * this.pageSize;
      let end = start + this.pageSize;
      return this.tableData.slice(start, end);
    },
  },
  created() {
    // 初始化表格数据
    (this.tableData = [
      {
        name: "zxiat.org",
        address: "高风险",
        deviceType: "路由器",
        ipAddress: "192.168.1.1",
        ipLocation: "河南,郑州",
        realLocation: "113.538912,34.820759",
        estimatedLocation: "113.545612,34.79131",
        industry: "教育行业",
      },

      {
        name: "example1.com",
        address: "低风险",
        deviceType: "服务器",
        ipAddress: "192.168.1.2",
        ipLocation: "广东，广州",
        realLocation: "113.264385,23.12911",
        estimatedLocation: "113.270085,23.10061",
        industry: "金融行业",
      },
      {
        name: "example2.com",
        address: "中风险",
        deviceType: "交换机",
        ipAddress: "192.168.1.3",
        ipLocation: "江苏，南京",
        realLocation: "118.796877,32.06031",
        estimatedLocation: "118.802577,32.03081",
        industry: "医疗行业",
      },
      {
        name: "example3.com",
        address: "高风险",
        deviceType: "防火墙",
        ipAddress: "192.168.1.4",
        ipLocation: "浙江，杭州",
        realLocation: "119.531762,29.87722",
        estimatedLocation: "119.537462,29.84772",
        industry: "制造业",
      },
      {
        name: "example4.com",
        address: "低风险",
        deviceType: "路由器",
        ipAddress: "192.168.1.5",
        ipLocation: "四川，成都",
        realLocation: "104.065735,30.57281",
        estimatedLocation: "104.071435,30.54331",
        industry: "科技行业",
      },
      {
        name: "example5.com",
        address: "中风险",
        deviceType: "打印机",
        ipAddress: "192.168.1.6",
        ipLocation: "福建，福州",
        realLocation: "119.296192,26.07451",
        estimatedLocation: "119.291492,26.04501",
        industry: "零售行业",
      },
    ]),
      (this.totalDataCount = this.tableData.length); // 设置总数据量
  },
  mounted() {
    this.getList();
  },
};
</script>

<style lang="scss" scoped>
.mainTable {
  padding: 15px;
  height: 85vh;
  display: flex;
  justify-content: space-around;
  flex-direction: column;
}

.flex-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* 垂直居中对齐 */
}

.btn {
  margin-left: 60px;
  background: linear-gradient(228deg, #ffc04b 0%, #ffa216 100%);
  color: #fff;
  border-radius: 4px 4px 4px 4px;
}

.flex-group-right {
  margin-left: 15px;
}

.rightBtn {
  margin-right: 55px;
  width: 106px;
  background: linear-gradient(270deg, #2dbffd 0%, #0e5fff 100%);
  border-radius: 4px 4px 4px 4px;
}

.textStyle {
  margin-left: 5px;
  width: 96px;
  height: 22px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 16px;
  color: #535a6c;
  line-height: 19px;
  text-align: left;
  font-style: normal;
  text-transform: none;
}

::v-deep .el-pager li.active {
  background: linear-gradient(270deg, #2dbffd 0%, #0e5fff 100%) !important;
  color: white !important;
}

.search-icon {
  cursor: pointer;
  font-size: 18px;
  vertical-align: middle;
}
::v-deep .el-pagination {
  float: inline-end;
}
</style>
